package core;


import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class RootStorageUnitTests {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@Test
	public void addDuplicateNameTest()
	{
		RootStorageUnit.removeInstance();
		StorageUnit resultUnit = null;
		RootStorageUnit root = RootStorageUnit.getInstance();
		StorageUnit su1 = new StorageUnit("SU1");
		StorageUnit su2 = new StorageUnit("SU2");
		
		assertTrue(root.getStorageUnitCount()==0);
		
		resultUnit = root.addStorageUnit(su1);
		assertTrue(resultUnit != null);
		resultUnit = root.addStorageUnit(su2);
		assertTrue(resultUnit != null);
		
		boolean thrown = false;
		StorageUnit su3 = new StorageUnit("SU2");
		try{
			resultUnit = root.addStorageUnit(su3);
		}
		catch(Exception e)
		{
			thrown = true;
		}
		assertTrue(thrown);
		
		su3.setName("SU3");
		resultUnit = root.addStorageUnit(su3);
		assertTrue(resultUnit != null);
	}
	
	@Test
	public void modifyNameWithErrorsTest()
	{
		StorageUnit resultUnit = null;
		RootStorageUnit root = RootStorageUnit.getInstance();
		StorageUnit su1 = new StorageUnit("SU4");
		StorageUnit su2 = new StorageUnit("SU5");

		resultUnit = root.addStorageUnit(su1);
		assertTrue(resultUnit != null);
		resultUnit = root.addStorageUnit(su2);
		assertTrue(resultUnit != null);
		
		assertTrue(root.getStorageUnitCount()==5);
		
		boolean thrown = false;
		try{
			root.editStorageUnit("SU5", "StorageUnit5");
		}
		catch(Exception e)
		{
			thrown = true;
		}
		assertFalse(thrown);
		
		thrown = false;
		try{
			root.editStorageUnit("SU5", "SU6");
		}
		catch(Exception e)
		{
			thrown = true;
		}
		assertTrue(thrown);
		
		thrown = false;
		try{
			root.editStorageUnit("StorageUnit5", "SU1");
		}
		catch(Exception e)
		{
			thrown = true;
		}
		assertTrue(thrown);
		
		thrown = false;
		try{
			root.editStorageUnit("StorageUnit5", "");
		}
		catch(Exception e)
		{
			thrown = true;
		}
		assertTrue(thrown);
	}

}
